<?php
/**
 * 后台商品标签的管理
 * Author: lyun
 * Date: 2018/4/3
 * Created by Panxsoft.
 */


namespace App\Http\Controllers\Admin\Panx\Product;

use App\Http\Controllers\ApiController;
use App\Http\Requests\Admin\Panx\Product\ProductTagStoreRequest;
use App\Http\Requests\Admin\Panx\Product\ProductTagUpdateRequest;
use App\Models\Admin\PanxAdmin;
use App\Models\Panx\ProductTag;
use App\Services\Utils\Transaction;
use Illuminate\Http\Request;

class ProductTagController extends ApiController
{
    const ROUTES = [
        'index'   => 'api.admin.panx.tag.index',
        'store'   => 'api.admin.panx.tag.store',
        'update'  => 'api.admin.panx.tag.update',
        'destroy' => 'api.admin.panx.tag.delete',
    ];

    /**
     * Display a listing of the resource.
     * @test
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function index(Request $request)
    {
        $this->checkIsTrue(PanxAdmin::checkIsSuperMan(), trans('admin.is_super_man'));

        $product_tags = ProductTag::latest()
            ->paginate($request->per_page?:15);

        return $this->response(compact('product_tags'));
    }


    /**
     * Store a newly created resource in storage.
     * @test
     *
     * @param  ProductTagStoreRequest  $request
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function store(ProductTagStoreRequest $request)
    {
        $this->checkIsTrue(PanxAdmin::checkIsSuperMan(), trans('admin.is_super_man'));

        $stored = ProductTag::create([
            'name'  => $request->name,
            'const' => $request->const
        ]);

        return $this->response(compact('stored'));
    }

    /**
     * Update the specified resource in storage.
     * @test
     *
     * @param  ProductTagUpdateRequest  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function update(ProductTagUpdateRequest $request, $id)
    {
        $this->checkIsTrue(PanxAdmin::checkIsSuperMan(), trans('admin.is_super_man'));

        /** @var ProductTag $product_tag */
        $product_tag = ProductTag::find($id);
            $this->checkItemExist($product_tag, trans('admin.product_tag'));

        $updated = $product_tag->update([
            'name'  => $request->name,
            'const' => $request->const
        ]);

        return $this->response(compact('updated'));
    }

    /**
     * Remove the specified resource from storage.
     * @test
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function destroy($id)
    {
        $this->checkIsTrue(PanxAdmin::checkIsSuperMan(), trans('admin.is_super_man'));

        /** @var ProductTag $product_tag */
        $product_tag = ProductTag::find($id);
            $this->checkItemExist($product_tag, trans('admin.product_tag'));
            $this->checkTagPivotProducts($product_tag);

        $deleted = $this->transactDelete($product_tag);

        return $this->response(compact('deleted'));
    }

    /**
     * @param ProductTag $product_tag
     * @return mixed
     * @throws \Exception
     */
    private function transactDelete(ProductTag $product_tag)
    {
        return Transaction::handle(function () use ($product_tag) {
            $product_tag->products()->sync([]);
            $product_tag->skinDetectionItems()->sync([]);

            return $deleted = $product_tag->delete();
        });
    }

    /**
     * @param ProductTag $product_tag
     * @throws \Exception
     */
    private function checkTagPivotProducts(ProductTag $product_tag): void
    {
        $relate_prod_num = $product_tag->products()->count();
        if ($relate_prod_num) {
            $this->requestForceOperate(trans('admin.relate_prod', ['num'=>$relate_prod_num]));
        }
    }
}
